/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package pe.edu.pucp.cpnc.mm.servimp;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import javax.swing.ImageIcon;
import pe.edu.pucp.cpnc.mm.bean.Tile;
import pe.edu.pucp.cpnc.mm.service.MMServices;
import pe.edu.pucp.cpnc.mm.bean.Map;
import pe.edu.pucp.cpnc.mm.bean.Object;
import pe.edu.pucp.cpnc.mm.manager.CmdManager;
import pe.edu.pucp.cpnc.mm.renderer.Renderer;

/**
 *
 * @author alulab11
 */
public class MMServicesImp extends UnicastRemoteObject implements MMServices {
    private  CmdManager cmd;
    private  Renderer rd;
    private Map map;
    private int clientCount;
    
    public MMServicesImp() throws RemoteException{
        cmd = new CmdManager();
        rd = new Renderer();
        map = null;
        clientCount = 0;
    }
    
    public MMServicesImp(CmdManager cmd) throws RemoteException{
        this.cmd = cmd;
        rd = new Renderer();
        map=null;
        clientCount = 0;
    }

    @Override
    public int load(String file) throws RemoteException {
        if(map != null){
            System.out.println("Error: There is a map already loaded in server memory.");
            return -1;
        }
        map = cmd.load(file);
        if(map == null) return -1;
        else return 0;
    }

    @Override
    public void save(String file) throws RemoteException {
        if(map==null){
            System.out.println("Error: No maps loaded in server memory.");
            return;
        }
        cmd.save(map, file);
    }

    @Override
    public pe.edu.pucp.cpnc.mm.resdb.ManagerDB getResources() throws RemoteException{
        return CmdManager.getDb();
    }

    @Override
    public void changeTile(int posX, int posY, Tile tile) throws RemoteException {
        cmd.changeTile(posX, posY, tile, map);
    }

    @Override
    public int addObject(int posX, int posY, Object object, int idPlayer) throws RemoteException {
        return cmd.addObject(posX, posY, object, map, idPlayer);
    }

    @Override
    public void eraseObject(int posX, int posY) throws RemoteException {
        cmd.eraseObject(posX, posY, map);
    }

    @Override
    public void updateObject(int posX, int posY, int newPosX, int newPosY) throws RemoteException {
        cmd.updateObject(posX, posY, newPosX, newPosY, map);
    }

    @Override
    public ImageIcon printMap(int width, int height, int mouseX, int mouseY, pe.edu.pucp.cpnc.mm.bean.Object selObj) throws RemoteException {
        return rd.mapToImage(width, height, map, mouseX, mouseY, selObj);
    }

    @Override
    public void addPlayer(int id, char color, int type, String civilization, String name) throws RemoteException {
        cmd.addPlayer(id, color, type, civilization, name, map);
    }

    @Override
    public void createNewMap(int width, int height, String name, Tile t) throws RemoteException{
        map = new Map(width, height, name, t);
    }

    @Override
    public int checkMemState() throws RemoteException {
        if(map == null) return 0;
        else return 1;
    }

    @Override
    public void clientCountUpdate(int val) throws RemoteException {
        clientCount = clientCount + val;
        if(clientCount == 0) map = null;
    }
}
